<?php

class cntrlchartlist implements viewController {
	private $area;
	private $side;
	private $dependencies = array("JavaScript" => array("facebox.js"), "CSS" => array("facebox.css", "hideSidebar.css") );
	private $pageName = "PicBoard chart list";
	private $userObj;
	private $tools = "";
	
	private $mode = "user";

	public function __construct(user &$userObj){
		$this->userObj = $userObj;
		if($this->userObj->getAccessLevel() == -1 || $this->userObj->getAccessLevel() == ""){
			// User is not logged in
			header("Location: home");
		} else {
			$this->area = new template('area.listPage');	
			$this->side = "";
					
			parse_str( $_GET['act'] );
		
			
			$itemTemplate = new template("fragment.listDisplayItem");
			
			
			$page = isset($page) ? (int)$page : 1;
			$start = --$page * SYS_PAGE_MAXITEMS;	

			// Is this for the user's charts, or for a group?
			if( isset($g) ){
				// Is the user allowed to see this group?
				if($this->userObj->isInGroup($g)){
					$charts = chart::getChartList($g, $start, SYS_PAGE_MAXITEMS, "group");	
					$images = chart::getGroupChartThumbs( $g );
				} else {
					header("Location: home");
					exit;
				}
				
			} else {
				$charts = chart::getChartList($this->userObj->getuID(), $start, SYS_PAGE_MAXITEMS);	
				$images = chart::getUserChartThumbs( $this->userObj->getuID() );
			}
			
			
			$chartCount = $charts['FOUND_ROWS()'];			
			unset($charts['FOUND_ROWS()']);
	
			$numPages = ceil($chartCount / SYS_PAGE_MAXITEMS);
	
			$pagination = sprintf("<var id=\"lang_222_page\"></var> %d <var id=\"lang_221_of\"></var> %d. %d <var id=\"lang_224_total\" class=\"lower\"></var> <var id=\"lang_120_charts\" class=\"lower\"></var>.", (int)$page+1, $numPages, $chartCount );
			
			$gID = isset($g) ? "&g=$g" : "";
			
			$prevLink = (($page+1) == 1) ? "<var id=\"lang_185_previousPage\"></var>" : "<a href='chartlist?page=" . $page . "$gID'><var id=\"lang_185_previousPage\"></var></a>";
			$nextLink = (($page+1) == $numPages) ? "<var id=\"lang_223_nextPage\"></var>" : "<a href='chartlist?page=" . ($page+2) . "$gID'><var id=\"lang_223_nextPage\"></var></a>";
						
			$pageNav = sprintf("$prevLink | <var id=\"lang_222_page\"></var> %d | $nextLink", $page+1);
			
			$chartOutput = $this->formatChartsAndReturn( $itemTemplate, $charts, $images );	
			
			$this->area->replaceAll(array(
				'list' => $chartOutput,
				'pagination' =>	$pagination,
				'pageNav' => $pageNav
			));		
			
		}
	}


	private function formatChartsAndReturn(template &$itemTemplate, $charts, $images){
		$chartOutput = "";
				
			
		
		foreach($charts as $chart){
						
			$out = "<a href='chart?id=" . $chart['chID'] . "'>" . $chart['name'] . "</a> <a href='#' class='share_link'><img src='" . SYS_BASEURL ."/images/user--arrow.png' class='iconHelper' align='right' border='0' title='<var id='lang_188_shareChart'></var>' /></a>";
			
			if($chart['private'] == 1) $out .= "<img src='" . SYS_BASEURL ."/images/eye--exclamation.png' class='iconHelper' align='right' title='<var id='lang_189_privateChart'></var>' />";
			
			$chart['description'] = ($chart['description'] != "") ? "<p>" . $chart['description'] . "</p>" : "<p class='smgreyText'><var id=\"lang_201_noDesc\"></var></p>"; 
			
			$out .= $chart['description'];

			$imageURL = (array_key_exists($chart['chID'], $images)) ? $images[$chart['chID']] : SYS_BASEURL . "/images/document.png";
			
			// Is this IE & SVG?
			$browser = util::getBrowser();
			if(array_key_exists("msie", $browser) && strpos($imageURL, '.svg') !== FALSE ){
				$imageURL = preg_replace("/svg/i", 'wmf', $imageURL);

				$format = "<img src='%s' height='70' width='70' />";
				$imageTemplate = sprintf($format, $imageURL);	

			} else {
				// If this is a SVG we need to change the template.
				if(strpos($imageURL, '.svg') !== FALSE){
					// We have a SVG
					
					// We need to strip off the http:// of the URL.
					$imageURL = preg_replace("/(http:\/\/)/i", "", $imageURL);
					$format = '<embed src="http://access.ecs.soton.ac.uk/seb/PicBoard/SVGProxy.php?r=%s" type="image/svg+xml" height="70" width="70">';
			
				} else {
					$format = "<img src='%s' height='70' width='70' />";
				}
				$imageTemplate = sprintf($format, $imageURL);			
			}
						
			$itemTemplate->replace("desc", $out);
			$itemTemplate->replace("image", $imageTemplate);
			$chartOutput .= $itemTemplate->get();
			$itemTemplate->reset();
		}	
		return $chartOutput;
		
	}

	public function getSide(){
		return $this->side;
	}
	
	public function getArea(){
		return $this->area;
	}
	
	public function getDependencies(){
		return $this->dependencies;
	}

	public function getPageName(){
		return $this->pageName;
	}
	
	public function getTools(){
		return $this->tools;
	}	

}

?>